package com.intranet.sys.web;

import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.intranet.fund.domain.Visitors;
import com.intranet.fund.service.BlacklistService;
import com.intranet.fund.service.VisitorsService;
import com.intranet.sys.domain.Menu;
import com.intranet.sys.domain.User;
import com.intranet.sys.service.MenuService;
import com.intranet.sys.service.UserService;
import com.water.cons.CommonConstant;
import com.water.listener.Config;
import com.water.web.BaseController;

/**
 * 用户登录
 * 
 * @author whl
 * 
 */
@Controller
public class LoginController extends BaseController {

	@Autowired
	private UserService userService;
	@Autowired
	private MenuService menuService;
	@Autowired
	private VisitorsService visitorsService;
	@Autowired
	private BlacklistService blacklistService;

	/**
	 * 欢迎页面
	 * 
	 * @param request
	 * @param user
	 * @return
	 */
	@RequestMapping("/welcome")
	public ModelAndView welcome(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		String ip = getIpAddr(request); // IP地址
		//过滤IP黑名单
		if(blacklistService.findBlacklistByIp(ip)){
			mav.setViewName("/fund/blacklist");
		}else{
			mav.setViewName("/fund/welcome");
		}
		
		// 记录访客信息
		Visitors v = new Visitors();
		Properties p = Config.getProps();      
		String osName = System.getProperty("os.name"); // 操作系统名称
		String agent = request.getHeader("user-agent");// 获取浏览器名称
		v.setOsName(osName);
		v.setAgent(agent);
		v.setIp(ip);
		Date date = new Date();
		v.setAccessTime(date);
		visitorsService.saveVisitors(v);
		
		
		return mav;
	}

	public String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	/**
	 * 登陆页面
	 * 
	 * @param request
	 * @param user
	 * @return
	 */
	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/sys/login");
		return mav;
	}

	/**
	 * 用户登录
	 * 
	 * @param request
	 * @param user
	 * @return
	 */
	@RequestMapping("/submit")
	public ModelAndView submit(HttpServletRequest request, User user) {
		ModelAndView mav = new ModelAndView();
		User dbUser = userService.getUserByUserName(user.getUsername());
		if (dbUser != null && dbUser.getPassword().equals(user.getPassword())) {
			List<Menu> menus = menuService.findMenuByPId(0);
			setSessionLeftMenus(request, menus);
			setSessionUsername(request, dbUser.getUsername());
			mav.setViewName("/sys/index");
		} else {
			mav.addObject("message","对不起，用户名或密码错误!");
			mav.setViewName("/sys/login");
		}
		return mav;
	}

	/**
	 * 登录注销
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("/doLogout")
	public String logout(HttpSession session) {
		session.removeAttribute(CommonConstant.USER_CONTEXT);
		return "forward:/water.jsp";
	}

}
